1237B - Balanced Tunnel - CodeForces Solution


data structures sortings two pointers *1300

Please click on ads to support us..

Python Code:

n = int(input())
lst_in = list(map(int,input().split()))
lst_out = list(map(int,input().split()))
i = 0 
j = 0
s = set()
while j < n:
  if lst_in[i] in s:
    i+=1
  elif lst_in[i] != lst_out[j]:
    s.add(lst_out[j])
    j+=1
  else:
    i+=1
    j+=1
print(len(s))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define N 100010

int n;
bool hasQualified[N];
queue<int> initialOrder, finalOrder;

int main() {
    
    cin >> n;

    for (int i = 1; i <= n; i++) {
        int carNum;
        cin >> carNum;
        initialOrder.push(carNum);
    }

    for (int i = 1; i <= n; i++) {
        int carNum;
        cin >> carNum;
        finalOrder.push(carNum);
    }

    int numQualified = 0;
    while (finalOrder.size()) {
        while (hasQualified[initialOrder.front()])
            initialOrder.pop();

        if (initialOrder.front() == finalOrder.front()) {
	    	initialOrder.pop();
	    	finalOrder.pop();
		}

        else {
            hasQualified[finalOrder.front()] = true;
            finalOrder.pop();
            numQualified++;
        }
    }

    cout << numQualified << endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks